<?php


namespace app\common\helper;
use app\common\model\SchoolAdmin;
use GuzzleHttp\Client;
use think\helper\Str;

class SendSmsHelper
{
    protected $url;
    protected $dx_userid;
    protected $dx_password;
    protected $dx_username;
    protected $dx_sign;
    protected $username;
    protected $code; // 验证码
    protected $flag; // 随机串 防攻击
    protected $result; // 发送成功或失败
    protected $type; // 邮件类型 0注册 其它
    protected $smsData; // 短信内容
    public function __construct()
    {
        $param = cache('config');
        $this->dx_username = $param['ming_account'];
        $this->dx_password = $param['ming_password'];
        $this->dx_userid = $param['ming_id'];
        $this->dx_sign = $param['ming_sign']; // 短信签名 【快网科技】
    }

    public function sendSms($tel,$type,$body=null,$code=null)
    {
        if (empty($type)) {
            return ['code'=>0, 'msg'=>'未知错误'];
        }

        if (empty($code)) {
            $code = self::getCode();
        }

        if (empty($body)) {
            $body = "您的验证码为 {$code},请在30分钟内使用。";
        }

        if(!preg_match("/^1[2-9]{1}\d{9}$/",$tel)){
            return ['code'=>0, 'msg'=>'手机号格式错误'.$tel.'1'];
        }

        $result = $this->smsSend($tel,$body);
        $xml = simplexml_load_string($result);
        cache("{$tel}_{$type}",$code,60*30);
        return [
            'code' => $xml->returnstatus == 'Success' ? 1 : 0,
            'msg' => $xml->message
        ];
    }

    public function checkYzm($tel,$type,$code)
    {
        if (!$tel || !cache("{$tel}_{$type}")) {
            return false;
        }
        if ($code != cache("{$tel}_{$type}")) {
            return  false;
        }
        cache("{$tel}_{$type}",null);
        return  true;
    }

    public function smsSend($phone,$body)
    {

        $_url = 'http://sms.37037.com/v2sms.aspx';

        $timestamp = date('YmdHis');
        $sign = md5($this->dx_username.$this->dx_password.$timestamp);

        $post_data["userid"] = $this->dx_userid;
        $post_data["timestamp"] = $timestamp;
        $post_data["sendTime"] = "";
        $post_data["action"] = "send";
        $post_data["extno"] = "";
        $post_data["sign"] = $sign;
        $post_data["content"] = $this->dx_sign . $body;
        $post_data["mobile"] = $phone;
        $this->smsData = $post_data;
        $client = new Client();
        $result = $client->post($_url,["form_params"=>$post_data])->getBody();
        return $result;
    }
    // 设置用户名
    public function setUserName($username)
    {
        $this->username = $username;
    }
    // 获取验证码
    public function getCode()
    {
        if (empty($this->code)) {
            self::setCode();
        }
        return $this->code;
    }
    // 设置验证码
    public function setCode($code = null)
    {
        if (empty($code)) {
            $this->code = Str::random(4,1);
        } else {
            $this->code = $code;
        }
    }
    // 设置随机字条串
    public function setFlag($flag)
    {
        $this->flag = $flag;
    }
    public function getFlag()
    {
        return $this->flag;
    }
    // 设置邮件类型
    public function setType($type)
    {
        $this->type = $type;
    }
    public function getType()
    {
        return $this->type;
    }
}